package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.AreaDao;
import com.jiantong.user.model.Area;
import com.jiantong.user.service.AreaReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 31/5/17.
 *
 */
@Slf4j
@Service
@RpcProvider
public class AreaReadServiceImpl implements AreaReadService {

    private final AreaDao areaDao;

    @Autowired
    public AreaReadServiceImpl(AreaDao areaDao){
        this.areaDao = areaDao;
    }

    @Override
    public Response<List<Area>> queryList(String areaCode, String grade) {
        try{
            Map<String , Object> map = Maps.newHashMap();
            if(areaCode != null){
                switch (grade){
                    case "1":
                        LoginUser loginUser = UserUtil.getCurrentUser();
                        Integer provinceId = loginUser.getUserProfile().getProvinceId();
                        String excludeAreaCode = "";
                        if(provinceId < 10) excludeAreaCode = "0" + provinceId;
                        else excludeAreaCode = provinceId + "";
                        map.put("excludeAreaCode",excludeAreaCode);
                        break;
                    case "2":
                        map.put("areaCode", areaCode.substring(0,2));
                        break;
                    case "3":
                        map.put("areaCode", areaCode.substring(0,4));
                        break;
                    case "4":
                        map.put("areaCode", areaCode.substring(0,6));
                        break;
                    case "5":
                        map.put("areaCode", areaCode.substring(0,9));
                    default:
                        break;
                }

            }
            map.put("areaLevel",grade);
            return Response.ok(areaDao.queryList(map));
        }catch (Exception e){
            log.error("failed to find area list areaCode = {} , grade = {}",
                    areaCode,grade, Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find area list");
        }
    }

    @Override
    public Response<Map> selectids(Map criteria) {
        try{
           return Response.ok(areaDao.selectids(criteria));
        }catch (Exception e){
            log.error("failed to find area list areaCode = {} , grade = {}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find area list");
        }
    }

    @Override
    public Response<List<Area>> selectsubordinate(Map criteria) {
        try{
            return Response.ok(areaDao.selectsubordinate(criteria));
        }catch (Exception e){
            log.error("failed to find area list areaCode = {} , grade = {}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find area list");
        }
    }
    @Override
    public Response<Area> selectByAreaCode(Map criteria){
        try{
            return Response.ok(areaDao.selectByAreaCode(criteria));
        }catch (Exception e){
            log.error("failed to find area list areaCode = {} , grade = {}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find area list");
        }
    }

    @Override
    public Response<Map> selectLastCode(Map criteria) {
        try{
            return Response.ok(areaDao.selectLastCode(criteria));
        }catch (Exception e){
            log.error("failed to find lastCode = {} , grade = {}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find lastCode");
        }
    }



}
